package com.inplaceedit.client.ui.contact;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ProvidesKey;
import com.core.gwt.ui.client.IsFocusable;
import com.inplaceedit.shared.dto.CompanyDto;

public class ContactNameEditorView extends Composite implements
		ContactNameEditor, IsFocusable {

	interface Binder extends UiBinder<Widget, ContactNameEditorView> {
	}

	private static Binder uiBinder = GWT.create(Binder.class);

	@UiField
	TextBox firstName;
	@UiField
	TextBox lastName;
	@UiField
	TextBox title;
	@UiField
	DeckPanel companyPanel;
	@UiField(provided = true)
	ValueListBox<CompanyDto> company;

	private Handler handler;

	private final AsyncCallback<List<CompanyDto>> refreshCallback = new AsyncCallback<List<CompanyDto>>() {
		@Override
		public void onFailure(final Throwable caught) {
			companyPanel.showWidget(2);
		}

		@Override
		public void onSuccess(final List<CompanyDto> result) {
			company.setAcceptableValues(result);
			companyPanel.showWidget(0);
		}
	};

	private final Renderer<CompanyDto> companyRenderer = new AbstractRenderer<CompanyDto>() {
		@Override
		public String render(final CompanyDto object) {
			String result = "<none>";
			if (object != null) {
				result = object.getName();
			}
			return result;
		}
	};

	private final ProvidesKey<CompanyDto> companyKeyProvider = new ProvidesKey<CompanyDto>() {
		@Override
		public Object getKey(final CompanyDto item) {
			String result = null;
			if (item != null) {
				result = item.getId();
			}
			return result;
		}
	};

	public ContactNameEditorView() {
		company = new ValueListBox<CompanyDto>(companyRenderer,
				companyKeyProvider);

		initWidget(uiBinder.createAndBindUi(this));

		companyPanel.showWidget(0);
	}

	@Override
	public Focusable asFocusable() {
		return firstName;
	}

	@Override
	public LeafValueEditor<CompanyDto> getCompanySelector() {
		return company.asEditor();
	}

	@Override
	public LeafValueEditor<String> getFirstNameEditor() {
		return firstName.asEditor();
	}

	@Override
	public LeafValueEditor<String> getLastNameEditor() {
		return lastName.asEditor();
	}

	@Override
	public LeafValueEditor<String> getTitleEditor() {
		return title.asEditor();
	}

	@Override
	public void refresh() {
		if (handler != null) {
			companyPanel.showWidget(1);
			handler.getCompanyList(refreshCallback);
		}
	}

	@Override
	public void setHandler(final Handler handler) {
		this.handler = handler;
	}

	@UiHandler("firstName")
	void onFirstNameFocus(final FocusEvent event) {
		firstName.selectAll();
	}

	@UiHandler("lastName")
	void onLastNameFocus(final FocusEvent event) {
		lastName.selectAll();
	}

	@UiHandler("title")
	void onTitleFocus(final FocusEvent event) {
		title.selectAll();
	}
}
